package com.yh.freemarker.controller;

import com.yh.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Controller
public class FreemarkerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("tobanner")
    public String toBanner(Map map){
        ResponseEntity<Map> entity = restTemplate
                .getForEntity("http://localhost:31001/cms/config/getmodel/5a791725dd573c3574ee333f", Map.class);
        Map body = entity.getBody();
        map.putAll(body);
        return "index_banner";
    }

    @GetMapping("toCourse")
    public String toCourse(Map map){
        ResponseEntity<Map> entity = restTemplate
                .getForEntity("http://localhost:31200/course/courseview/4028e58161bcf7f40161bcf8b77c0000", Map.class);
        Map body = entity.getBody();
        map.putAll(body);
        return "course";
    }

    @GetMapping("test1")
    public String toStu(Map map){
        //向数据模型放数据
        map.put("name","Ty");
        List<Student> list = new ArrayList<>();
        Student student = new Student();
        student.setAge(18);
        student.setMoney(15000.0f);
        student.setName("王志豪");
        student.setBirthday(new Date());
        list.add(student);
        Student student2 = new Student();
        student2.setAge(18);
        student2.setMoney(55000.0f);
        student2.setName("Ty");
        student2.setBirthday(new Date());
        list.add(student2);
        map.put("stus",list);

        //准备map数据
        Map hashMap = new HashMap();
        hashMap.put("stu1",student);
        hashMap.put("stu2",student2);
        map.put("stuMap",hashMap);
        return "test1";//  /templates/stu.ftl
    }

}
